package com.cjs.hegui30;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText addressInput;
    private EditText portInput;
    private TextView statusText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addressInput = findViewById(R.id.addressInput);
        portInput = findViewById(R.id.portInput);
        statusText = findViewById(R.id.statusText);
        Button saveButton = findViewById(R.id.saveButton);

        // 加载当前配置
        loadCurrentConfig();

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveConfig();
            }
        });

        // 显示Xposed状态
        updateXposedStatus();
    }

    private void loadCurrentConfig() {
        String[] config = ConfigProvider.getConfig(this);
        addressInput.setText(config[0]);
        portInput.setText(config[1]);
    }

    private void saveConfig() {
        String address = addressInput.getText().toString().trim();
        String port = portInput.getText().toString().trim();

        if (address.isEmpty() || port.isEmpty()) {
            Toast.makeText(this, "地址和端口不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            int portNum = Integer.parseInt(port);
            if (portNum <= 0 || portNum > 65535) {
                Toast.makeText(this, "端口号必须在1-65535之间", Toast.LENGTH_SHORT).show();
                return;
            }
        } catch (NumberFormatException e) {
            Toast.makeText(this, "端口号必须是数字", Toast.LENGTH_SHORT).show();
            return;
        }

        ConfigProvider.saveConfig(this, address, port);
        Toast.makeText(this, "配置已保存", Toast.LENGTH_SHORT).show();
    }

    private void updateXposedStatus() {
        boolean active = isModuleActive();
        statusText.setText("Xposed状态: " + (active ? "已激活" : "未激活"));
        statusText.setTextColor(getResources().getColor(
                active ? android.R.color.holo_green_dark : android.R.color.holo_red_dark));
    }

    /**
     * 此方法会被HookTrack中的XC_MethodReplacement替换
     * 如果模块已激活，将返回true
     */
    private boolean isModuleActive() {
        return false;
    }
}
